<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class qqController extends Controller
{

    public function qq(Request $request)
    {

    // Step2：获取Authorization Code
            header("Content-type: text/html; charset=utf-8");
//            $code = $_GET['code'];
           $code=$request->code;

           dd($code);
// Step3：通过Authorization Code获取Access Token
            $url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101392477&client_secret=db27885be67e5ea942e9e9eb09701cb0&code=' . $code . '&redirect_uri=http://www.tzvk.vip/login';
            $res = https_request($url);

// var_dump($res);
// 返回的数据
// access_token=YOUR_ACCESS_TOKEN&expires_in=3600
// exit;

// 切割字符串 获取access_token
            $res = explode('&', $res);
            $res = explode('=', $res[0]);

            $access_token = $res[1];


// Step4：使用Access Token来获取用户的OpenID

            $openid_url = 'https://graph.qq.com/oauth2.0/me?access_token=' . $access_token;
// var_dump($openid_url);
// exit;
            $openid = https_request($openid_url);


            if (strpos($openid, "callback") !== false) {
                $lpos = strpos($openid, "(");
                $rpos = strrpos($openid, ")");
                $openid = substr($openid, $lpos + 1, $rpos - $lpos - 1);
            }
            $user = json_decode($openid, true);

            $openid_info = $user['openid'];


            $userinfo_url = 'https://graph.qq.com/user/get_user_info?access_token=' . $access_token . '&oauth_consumer_key=[你的APPID]&openid=' . $openid_info;

            $userinfo = https_request($userinfo_url);
            echo "<pre>";
            print_r($userinfo);
            dd($userinfo);


        }

    function https_request($url,$data)
    {
// curl 初始化
        $ch = curl_init();

// 参数设置
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

// 这里进行 判断
        if ( !empty($data) ) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行
        $res = curl_exec($ch);
// curl_close($ch);
        return $res;
    }

}
